package com.zzyl.serve.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.serve.domain.Device;
import com.zzyl.serve.dto.DeviceDto;
import com.zzyl.serve.service.IDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 智能设备Controller
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "智能设备的接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('elder:device:list')")
    @GetMapping("/list")
    @ApiOperation("查询设备列表")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    //    @GetMapping("/allProduct")
//    @ApiOperation(value = "查询所有产品列表")
//    public AjaxResult allProduct() {
//        return success(deviceService.allProduct());
//    }
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    public AjaxResult allProduct() {
        return success(deviceService.allProduct());
    }


    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public AjaxResult registerDevice(@RequestBody DeviceDto dto) {
        deviceService.registerDevice(dto);
        return success();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "iotId", value = "物联网设备id", required = true, dataTypeClass = String.class)})
    public AjaxResult getInfo(@PathVariable("iotId") String iotId) {
        return success(deviceService.queryDeviceDetail(iotId));
    }

    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation(value = "查看设备上报数据")
    public AjaxResult queryServiceProperti(@PathVariable("iotId") String iotId) {
        List<Map>  ajaxResult = deviceService.queryServiceProperti(iotId);
        return AjaxResult.success( ajaxResult);
    }

     /*
     * 修改设备
     * */
    @PutMapping ()
    @ApiOperation(value ="修改设备")
    public AjaxResult editDevice(@RequestBody DeviceDto dto){
             deviceService.editDevice(dto);
        return success();
    }

    /*
     * 删除设备
     * */
    @DeleteMapping("/{iotId}")
    @ApiOperation(value ="修改设备")
    public AjaxResult deleteDevice(@PathVariable("iotId") String iotId){
        deviceService.deleteDevice(iotId);
        return success();
    }


}